package mrusanov.tournament

import mrusanov.team.Team
import java.util.Date
import mrusanov.battle.Battle
import mrusanov.buildings.Arena

class MatchInfo(val host: Team, val guest: Team, val time: Date, val battle: Battle) {
	private var _over: Boolean = false
	var hostPoints: Int = 0
	var guestPoints: Int = 0

	def isOver = _over

	def this(hostTeam: Team, guestTeam: Team, matchTime: Date) =
		this(hostTeam, guestTeam, matchTime, new Battle(hostTeam, guestTeam, hostTeam.club.buildings.arena))

	def setOver() {
		_over = true
	}

	def winner: Team = {
		require(isOver, "Unable to decide who is winner, match is not isOver")
		require(hostPoints != guestPoints, "Unable to decide who is winner, they have same amount of points")

		if (hostPoints > guestPoints) {
			host
		} else {
			guest
		}
	}

	def tookPart(club: Team): Boolean = club == guest || club == host

	def difference(club: Team): Int = {
		require(club == host || club == guest)

		if (club == host) {
			hostPoints - guestPoints
		} else {
			guestPoints - hostPoints
		}
	}

	def loser: Team = {
		if (host == winner) {
			guest
		} else {
			host
		}
	}

	override def toString: String = {
		if (!isOver) {
			host + " " + guest
		} else {
			host + " " + guest + " " + hostPoints + ":" + guestPoints
		}
	}

}
